/*
 *  $Id: Regeneration.cs 1189 2010-11-12 14:40:06Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

using Ragima.Helper;

namespace Ragima.Affects {
    /// <summary>
    /// Эффект: регенерация на X (число. если 0, то полная регенерация)
    /// Регенерирует X здоровья в начале фазы атаки.
    /// </summary>
    public class Regeneration : FlagAffect {
        private static readonly Languages.Phrase FullRegen = new Languages.Phrase("affect.regeneration.full");

        public readonly int Value;

        public Regeneration() {
            Value = 0;
        }
        public Regeneration(int value) {
            Value = value;
        }

        public override string Id {
            get { return "regeneration"; }
        }
        public override Languages.Phrase Name {
            get { return Value > 0 ? base.Name : FullRegen; }
        }
        public override string NameText {
            get { return Name.GetText(Value); }
        }

        public override int BaseWeight {
            get { return Value > 0 ? Value * 20 : 100; }
        }

        public override AffectIcon Icon {
            get { return AffectIcon.Regeneration; }
        }

        public override Affect Clone() {
            return new Regeneration(Value);
        }

        public override void SetAffect() {
            base.SetAffect();
            LocalSummary.OnStartOffencePhase += DoRegeneration;
        }
        public override void ResetAffect() {
            base.ResetAffect();
            LocalSummary.OnStartOffencePhase -= DoRegeneration;
        }

        private void DoRegeneration() {
            if (!Owner.HasFlag<Regeneration>())
                return;
            int value = Value > 0 ? Value : Owner.MaxHealth;
            if (Owner.HealthToMax != 0) {
                Region region = Owner.FindRegion();
                if (region != null) {
                    UI.ParticleInfo info = new UI.ParticleInfo(Data.Particles.Vlines01);
                    info.AddPhase(0, 0, 0x38E5FF.ToColor(), 0.1, 0.7, 0, UI.ParticleTrajectory.Line(-90, 0.8));
                    info.AddPhase(20, 40, 0x4da1FF.ToColor(), 0.5, 0.9);
                    info.AddPhase(30, 0, 0xFFFFFF.ToColor(), 0.1);
                    UI.ParticleEmitterInfo emitter = new UI.ParticleEmitterInfo();
                    emitter.Duration = 70;
                    emitter.Rate = UI.ParticleRate.Always(10);
                    emitter.Generator = UI.ParticleGenerator.Same(info, UI.ParticlePoints.EngulfFromBottom(region.Rect, 2));
                    Application.CurrentScene.AddEmitter(emitter.Create());
                }
            }
            Owner.HealBy(NewDamage(DamageType.Regeneration, value));
        }
    }
}
